using Infrastructure.Attribute;
using Infrastructure.Extensions;
using ZR.Repository;
using System.Linq;
using ZR.Service.IService.ProductionControl;
using ZR.Model.Models.ProductionControl;
using ZR.Model.Dto.Business.ProductionControl;
using ZR.Model;
using CKS.Core.Framework;

namespace ZR.Service.Service.ProductionControl
{
    /// <summary>
    /// Service业务层处理
    /// </summary>
    [AppService(ServiceType = typeof(IDcCoverrivetingassembleService), ServiceLifetime = LifeTime.Transient)]
    public class DcCoverrivetingassembleService : BaseService<DcCoverrivetingassemble>, IDcCoverrivetingassembleService
    {
        /// <summary>
        /// 查询列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public PagedInfo<DcCoverrivetingassembleDto> GetList(DcCoverrivetingassembleQueryDto parm)
        {
            var predicate = QueryExp(parm);

            var response = Queryable()
                .Where(predicate.ToExpression())
                .OrderByDescending(a=>a.AssembleTime)
                .ToPage<DcCoverrivetingassemble, DcCoverrivetingassembleDto>(parm);

            return response;
        }


        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public DcCoverrivetingassemble GetInfo(long Id)
        {
            var response = Queryable()
                .Where(x => x.Id == Id)
                .First();

            return response;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public DcCoverrivetingassemble AddDcCoverrivetingassemble(DcCoverrivetingassemble model)
        {
            return Insertable(model).ExecuteReturnEntity();
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateDcCoverrivetingassemble(DcCoverrivetingassemble model)
        {
            return Update(model, true);
        }

        /// <summary>
        /// 查询导出表达式
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        private static Expressionable<DcCoverrivetingassemble> QueryExp(DcCoverrivetingassembleQueryDto parm)
        {
            var exp = Expressionable.Create<DcCoverrivetingassemble>();
            exp.AndIF(parm.BeginTime != DateTime.MinValue && parm.BeginTime != null, u => u.AssembleTime >= parm.BeginTime.ToBeginTime());
            exp.AndIF(parm.EndTime != DateTime.MinValue && parm.EndTime != null, u => u.AssembleTime <= parm.EndTime.ToEndTime());
            exp.AndIF(!parm.CoverCode.IsEmpty(), u => u.CoverCode == parm.CoverCode);
           
            return exp;
        }
    }
}